package com.yoomiito.app.present.home;

import com.yoomiito.app.base.App;
import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseLazyFragmentPresenter;
import com.yoomiito.app.model.CreditCard;
import com.yoomiito.app.model.bean.BannerAndIconInfo;
import com.yoomiito.app.model.bean.GoodsInfo;
import com.yoomiito.app.model.bean.HomeActData;
import com.yoomiito.app.model.home.FreeBuyActInfo;
import com.yoomiito.app.model.home.HomeHotActData;
import com.yoomiito.app.model.home.PanicData;
import com.yoomiito.app.model.home.PanicGoods;
import com.yoomiito.app.model.home.PanicTime;
import com.yoomiito.app.model.home.VideoClipInfo;
import com.yoomiito.app.model.zhongtai.ZhongTaiData;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.fragment.HomeRecommendFragment;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.NetworkUtils;
import com.yoomiito.app.utils.YMToast;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;

public class HomeRecommendPresenter extends BaseLazyFragmentPresenter<HomeRecommendFragment> {

    public HomeRecommendPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void getFreeOilCard(){
//        YmApi.getYmService()
//                .getOilCard()
//                .compose(getV().bindToLifecycle())
//                .compose(XApi.getApiTransformerNoData())
//                .compose(XApi.getScheduler())
//                .subscribe(new ApiSubscriber<BaseModel>() {
//                    @Override
//                    public void onSuccess(BaseModel baseModel) {
//                        //弹窗
//                        getV().toAddOil();
//                    }
//
//                    @Override
//                    public void onFail(ApiException error) {
//                        getV().dismissLoading();
//                        if (error!=null){
//                            getV().toAddOilError(error.getCode());
//                        }
//                    }
//                });
    }

    /**
     * 获取banner 活动等
     */
    public void getBannerAndIcon() {
        YmApi.getYmService()
                .getHomeBannerAndIcon()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<HomeActData>getApiTransformerData())
                .compose(XApi.<HomeActData>getScheduler())
                .subscribe(new ApiSubscriber<HomeActData>(handleException()) {
                    @Override
                    public void onSuccess(HomeActData bannerAndIconInfo) {
                        getV().bannerData((ArrayList<BannerAndIconInfo>) bannerAndIconInfo.getBanner_list());
//                        handleOtherData(bannerAndIconInfo);
                        getV().setTabData(bannerAndIconInfo.getIcon_list());
//                        getV().setBaoPinData(bannerAndIconInfo.getProduct_hot().getData());
                        getV().setChosenActData(bannerAndIconInfo.getSpecial());

                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    public void getFeaturedArea() {
        YmApi.getYmService()
                .getFeaturedArea()
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<List<BannerAndIconInfo>>(handleException()) {
                    @Override
                    public void onSuccess(List<BannerAndIconInfo> featuredAreaInfos) {
                        getV().setFeaturedAreaData(featuredAreaInfos);
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    private void handleOtherData(HomeActData homeBannerAndIcon) {
//        List<BannerAndIconInfo> datas = getV().datas;
//        List<BannerAndIconInfo> iconInfos = homeBannerAndIcon.getIcon_list();
//        List<BannerAndIconInfo> chosen = homeBannerAndIcon.getChosen_list();
////        List<BannerAndIconInfo> hots = homeBannerAndIcon.getActivity_list().getHot();
//
//        //添加图标
//        setRcyData(datas,iconInfos, MultipleItem.TAB);
//
//        //添加VIP
//        if (homeBannerAndIcon.getVip()!=null){
//            BannerAndIconInfo bannerAndIconInfo = new BannerAndIconInfo();
//            HomeVipInfo vipInfo = homeBannerAndIcon.getVip();
////            bannerAndIconInfo.setBg_url(vipInfo.getVip_img());
//            bannerAndIconInfo.setValue(vipInfo.getVip_url());
//            bannerAndIconInfo.setItemType(MultipleItem.VIP);
//            datas.add(bannerAndIconInfo);
//        }
//
//        //
////        setRcyData(datas,chosen.subList(0,3), MultipleItem.LIMITED_TIME);
//        if (chosen!=null&&chosen.size()!=0){
//            Chosen chosen1 = new Chosen(chosen.get(0).getTitle(),
//                    chosen.get(1).getTitle(),
//                    chosen.get(2).getTitle(),
//                    chosen.get(3).getTitle(),
//                    chosen.get(0).getImage_url(),
//                    chosen.get(1).getImage_url(),
//                    chosen.get(2).getImage_url(),
//                    chosen.get(3).getImage_url());
//            BannerAndIconInfo bannerAndIconInfo2 = new BannerAndIconInfo();
//            bannerAndIconInfo2.setChosen(chosen1);
//            bannerAndIconInfo2.setItemType(MultipleItem.LIMITED_TIME);
//            datas.add(bannerAndIconInfo2);
//        }
//
//
//
//
//        //添加活动
////        if (hots!=null&&hots.size()!=0){
////            BannerAndIconInfo bannerAndIconInfo1 = new BannerAndIconInfo();
////            bannerAndIconInfo1.setValue("热门活动");
////            bannerAndIconInfo1.setItemType(MultipleItem.TEXT);
////            datas.add(bannerAndIconInfo1);
////            setRcyData(datas,hots, MultipleItem.ACTIVITY);
////        }

    }

    private void setRcyData(List<BannerAndIconInfo> datas, List<BannerAndIconInfo> infos, int type) {
        if (infos == null) {
            return;
        }

        for (BannerAndIconInfo info : infos) {
            info.setItemType(type);
        }
        datas.addAll(infos);
    }

    public void getRecommendGoods(int page,String id) {
        if (NetworkUtils.checkNetPermission(getV().getContext())) {
            getV().finishRefresh();
            return;
        }
        YmApi.getYmService()
                .getHomeRecommendGoods()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<List<GoodsInfo>>getApiTransformerData())
                .compose(XApi.<List<GoodsInfo>>getScheduler())
                .subscribe(new ApiSubscriber<List<GoodsInfo>>(handleException()) {
                    @Override
                    public void onSuccess(List<GoodsInfo> infos) {
                        getV().recommendGoods(infos, true);
                    }

                    @Override
                    public void onFail(ApiException error) {
                    }
                });
    }

    public void getPanicGoods(int type) {
//        YmApi.getYmService()
//                .getPanicGoods(type)
//                .compose(getV().bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(new ApiSubscriber<PanicData>(handleException()) {
//                    @Override
//                    public void onSuccess(PanicData panicData) {
//                        List<PanicGoods> panicGoods = panicData.getData();
//                        for (PanicGoods panicGoods1 : panicGoods) {
//                            panicGoods1.setClick(panicData.getHour_type_now() < panicData.getHour_type() ? false : true);
//                        }
//                        getV().setPanicGoodsData(panicData);
//                    }
//
//                    @Override
//                    public void onFail(ApiException error) {
//
//                        if (error.getCode() == 4001) {
//                            getV().setPanicGoodsEmpty();
//                        }
//                    }
//                });
    }

    private String[] time = {"00:00", "10:00", "12:00", "15:00", "20:00"};
    private String[] status = {"昨日开抢", "已开抢", "抢购中", "即将开抢", "明日开抢"};

    List<PanicTime> panicTimes = new ArrayList<>();

    public List<PanicTime> timeData(int currentTime) {

        if (panicTimes.size() == 0) {
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < time.length; j++) {

                    if (i == 0) {//昨日
                        panicTimes.add(new PanicTime(time[j], status[0], j + 1, false));
                    } else if (i == 1) {//今日
                        int statusPosition;
                        int p0 = j + 5;
                        int p1 = currentTime - 1;
                        if (p0 == p1) {
                            statusPosition = 2;
                        } else if (p0 < p1) {
                            statusPosition = 1;
                        } else {
                            statusPosition = 3;
                        }
                        panicTimes.add(new PanicTime(time[j], status[statusPosition], 5 + j + 1, p0 == p1 ? true : false));
                    } else {
                        panicTimes.add(new PanicTime(time[j], status[4], 10 + j + 1, false));
                    }
                }
            }
        } else {
            for (int i = 0; i < panicTimes.size(); i++) {
                PanicTime panicTime = panicTimes.get(i);
                boolean flag = i == currentTime - 1;
                panicTime.setSelected(flag ? true : false);
                if (i > 4 && i < 10) {
                    if (currentTime - 1 > i) {
                        panicTime.setStatus("已开抢");
                    }
                    if (currentTime - 1 < i) {
                        panicTime.setStatus("即将开抢");
                    }
                    if (flag)
                        panicTime.setStatus("抢购中");
                }
            }
        }


        return panicTimes;
    }

    public void getSystemInfo(){
        YmApi.getYmService()
                .getSystemDialog()
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BannerAndIconInfo>() {
                    @Override
                    public void onSuccess(BannerAndIconInfo systemDialogInfo) {
                        getV().setSystemDialog(systemDialogInfo);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().setSystemDialog(null);
                    }
                });
    }

    public void getFreeBuyActInfo() {
        //优先级队列  PriorityQueue
//        Observable.zip(YmApi.getYmServiceNoErrorCode().getFreeBuyAct(), YmApi.getYmServiceNoErrorCode().getSystemDialog(), (freeBuyActInfoBaseModel, systemDialogInfoBaseModel) -> {
//            AllDialogModel allDialogManager = new AllDialogModel();
//            if (freeBuyActInfoBaseModel.getCode() == 200) {
//                FreeBuyActInfo info = freeBuyActInfoBaseModel.getData();
//                if (info != null) {
//                    allDialogManager.setFreeOrderModel(info);
//                }
//            } else if (freeBuyActInfoBaseModel.getCode() != ApiException.INELIGIBLE_USER) {
//                YMToast.showText(freeBuyActInfoBaseModel.getMsg());
//            }
//
//            if (systemDialogInfoBaseModel.getCode() == 200) {
//                SystemDialogInfo systemDialogInfo = systemDialogInfoBaseModel.getData();
//                if (systemDialogInfo != null) {
//                    allDialogManager.setSystemModel(systemDialogInfo);
//                }
//            }
//
//            return allDialogManager;
//        })
//                .compose(XApi.changeScheduler())
//                .doOnError(throwable -> {
//
//                })
//                .subscribe(allDialog -> getV().addDialogManager(allDialog));

        if (App.mAppConfig.getIsLogin()){
//            YmApi.getYmService()
//                    .getFreeBuyAct()
//                    .compose(getV().bindToLifecycle())
//                    .compose(XApi.getApiTransformerData())
//                    .compose(XApi.getScheduler())
//                    .subscribe(new ApiSubscriber<FreeBuyActInfo>() {
//                        @Override
//                        public void onSuccess(FreeBuyActInfo freeBuyActInfo) {
//                            getV().setFreeBuyGoods(freeBuyActInfo);
//                        }
//
//                        @Override
//                        public void onFail(ApiException error) {
//                            getV().setFreeBuyGoods(null);
//                            if (error.getCode()!=ApiException.INELIGIBLE_USER){
//                                YMToast.showText(error.getMsg());
//                            }
//                        }
//                    });
        }
    }

    public void getVideoList(){
//        YmApi.getYmService()
//                .homeVideoList()
//                .compose(getV().bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(new ApiSubscriber<List<VideoClipInfo>>() {
//                    @Override
//                    public void onSuccess(List<VideoClipInfo> videoClipInfos) {
//                        getV().showVideoList(videoClipInfos);
//                    }
//
//                    @Override
//                    public void onFail(ApiException error) {
//
//                    }
//                });
    }

    public void zhongtaiUrl(){
//        YmApi.getYmService()
//                .getZhongTaiUrl()
//                .compose(getV().bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(new ApiSubscriber<ZhongTaiData>() {
//                    @Override
//                    public void onSuccess(ZhongTaiData zhongTaiData) {
//                        getV().setZhongTai(zhongTaiData);
//                    }
//
//                    @Override
//                    public void onFail(ApiException error) {
//                        getV().dismissLoading();
//                    }
//                });
    }

    public void creditCardUrl(){
//        YmApi.getYmService()
//                .creditCardUrl()
//                .compose(getV().bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(new ApiSubscriber<CreditCard>(handleException()) {
//                    @Override
//                    public void onSuccess(CreditCard creditCard) {
//                        getV().setCreditCard(creditCard);
//                    }
//
//                    @Override
//                    public void onFail(ApiException error) {
//                        getV().dismissLoading();
//                    }
//                });

    }

    public void hotAct(){
//        YmApi.getYmService()
//                .homeHotAct()
//                .compose(getV().bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(new ApiSubscriber<HomeHotActData>() {
//                    @Override
//                    public void onSuccess(HomeHotActData homeHotAct) {
//                        getV().setHotActData(homeHotAct);
//                    }
//
//                    @Override
//                    public void onFail(ApiException error) {
//                        getV().dismissLoading();
//                    }
//                });
    }
}
